/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.schedule.executor.handler;


import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.hyts.guava.object.Objects;
import com.hyts.schedule.executor.executor.DefaultScheduleExecutor;
import com.hyts.schedule.executor.model.JobBean;

/** 
 * @title AbstractJob.java  
 * @package com.hyts.schedule.quartz.handler  
 * @description 抽象job类操作，用于实现Job信息的抽象方法进行构建服务任务调度操作  
 * @see org.quartz.Job
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年5月18日 
 * @version V1.0 
 */
public abstract class Task implements Job{

	/**  
	 * <p>日志输出器</p> 
	 * @fieldName logger
	 * @fieldType Logger
	 */ 
	private Logger logger = LoggerFactory.getLogger(Task.class);
	
	/**  
	 * <p>jobBean对象</p> 
	 * @fieldName jobBean
	 * @fieldType R
	 */ 
	private JobBean jobBean;
	
	/**  
	 * @constructor：AbstractJob  
	 * @param jobBean  
	 */ 
	public Task() {
		super();
	}
	
	/**  
	 * <b>TODO(方法功能的描述)</b>
	 * <p>TODO(这里用一句话描述这个方法的作用)</p>
	 * @return
	 * @exception
	 */ 
	public Task build(JobBean jobBean){
		this.jobBean = jobBean;
		return this;
	}
	
	/**  
	 * <b>execute方法</b>
	 * <p>执行操作任务具体实现</p>
	 * @param param
	 * @return
	 * @exception
	 */ 
	public abstract JobBean run(JobBean jobBean);
	
	/* 
	 * @param context
	 * @throws JobExecutionException
	 * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)  
	 * @exception
	 */ 
	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		logger.info("【coffice-toolbox-schedule】 - 【任务操作开始】");
		logger.info("【coffice-toolbox-schedule】【执行任务信息】:"+Objects.toString(context));
		String token = context.getJobDetail().getKey().getName()+"-"+context.getJobDetail().getKey().getGroup();
		JobBean bean = null;
		if(DefaultScheduleExecutor.exsit(token)) {
			bean = run(DefaultScheduleExecutor.get(token));
		}else {
			bean = run(new JobBean(JSONObject.
					parseObject(context.getJobDetail().getJobDataMap().getString(token)
					),context.getJobDetail().getKey().getName(),context.getJobDetail().getKey().getGroup()));
		
		}
		logger.info("【coffice-toolbox-schedule】【执行任务结果】:"+
				Objects.toString(bean));
		if(bean.getResultJson() != null){
			bean.setParamJson(bean.getResultJson());
		}
		DefaultScheduleExecutor.put(token, bean);
		logger.info("【coffice-toolbox-schedule】- 【任务操作结束】");
	}

	/**  
	 * @name jobBean's getter method
	 * @param none
	 * @return jobBean
	 */
	public JobBean getJobBean() {
		return jobBean;
	}
	
	
}
